package com.zhihuichongcheng.application.ui.cricle.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.smartlib.cmnObject.dialog.DialogOnClick;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.cricle.CricleTypeBean;
import com.zhihuichongcheng.application.ui.cricle.adapter.CricleTypeAdapter;
import com.zhihuichongcheng.application.util.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/11/5.
 */

public class CricleTypeActivity extends BaseActivity{
    private RecyclerView mRcvType;
    private List<CricleTypeBean> mListType;
    private CricleTypeAdapter mAdapter;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricle_type);
        initView();
        initData();
    }

    private void initView()
    {
        setTitle("类型选择");
        mRcvType = (RecyclerView)findViewById(R.id.rcv_cricleType);
        mAdapter = new CricleTypeAdapter(mContext);
        mRcvType.setLayoutManager(new LinearLayoutManager(mContext));
        mRcvType.addItemDecoration(new RecycleViewDivider(mContext,LinearLayoutManager.VERTICAL,2,getResources().getColor(R.color.color_d8d8d8)));
        mRcvType.setAdapter(mAdapter);
    }

    private void initData()
    {
        intent = getIntent();
        if(intent!=null&&intent.hasExtra("list"))
        {
            mListType = intent.getParcelableArrayListExtra("list");
        }
        mAdapter.setData(mListType);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnClickListener(new DialogOnClick<CricleTypeBean>() {
            @Override
            public void onClick(CricleTypeBean cricleTypeBean) {
               List<CricleTypeBean> list = cricleTypeBean.getSubCategories();
                if(list.size()>0)
                {
                    intent = new Intent(mContext,SubCricleTypeActivity.class);
                    intent.putParcelableArrayListExtra("list",(ArrayList)list);
                    startActivityForResult(intent,0);
                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                }
                else
                {
                    intent = new Intent();
                    intent.putExtra("id",cricleTypeBean);
                    setResult(Activity.RESULT_OK,intent);
                    finish();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK)
        {
            setResult(Activity.RESULT_OK,data);
            finish();
        }

    }
}
